var meterInfoData = [];
var stationKWHInfoData = [];
var flagDate = undefined;
var balanceInitSettingData = undefined;
var fixedElectricCharge = undefined;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$ajaxList('searchBalanceInitSetting', getBalanceInitSetting);
});
function getBalanceInitSetting(data){
	balanceInitSettingData = data.balanceInitSettingList[0]
	$("#balanceMonth").val($("#detailID", parent.document).val());
	$("#detailID", parent.document).val("");
	$("#defaultBalanceDay").val(balanceInitSettingData.balanceDay);
	$("#fixRatio").val(balanceInitSettingData.defaultFixedRatio);
	var month = $("#balanceMonth").val().substring(4,6);
	var lastMonth = month >= 10 ? (month - 1) : "0" + (month - 1);
	if(month == "01") {
		lastMonth = "12";
	}
	$("#BalanceBillName").val("电力结算单" + lastMonth + "月26日-" + month + "月25日");
	$ajaxList('chargingFixedPriceName', getChargingFixedPriceName);
}
function getChargingFixedPriceName(data) {
	for (var i = 0; i < data.length; i++) {
		if (data[i].enabled) {
			fixedElectricCharge = data[i].chargingPrice;
			$("#fixPrice").val(data[i].chargingPrice);
			break;
		}
	}
	search();
}
function search(){
	$("#costBalanceBillThead").show();
	$("#errorMessage").hide();
	$("#errorMessage").empty();
	$ajax('searchMeterInfo', 'costBalanceBillSearch', searchMeterInfo, "", searchMeterInfo);
}
function searchMeterInfo(data){
	$("#costBalanceBillThead").empty();
	var costBalanceBillTbody = $("#costBalanceBillTbody");
	costBalanceBillTbody.empty();
	createTableHead("costBalanceBillThead", costBalanceBillCreateTable);
	meterInfoData = data && data.meterInfoList || [];
	stationKWHInfoData = data && data.stationKWHInfoList || [];
	var meterInfoLength = meterInfoData.length;
	var stationKWHInfoLength = stationKWHInfoData.length;
	if (meterInfoLength > 0) {
		var chargingStationID = undefined;
		for(var i=0; i<meterInfoLength; i++){
			var rowspanCount = 0;
			chargingStationID = meterInfoData[i] && meterInfoData[i].chargingStationID;
			for (var j=0; j<stationKWHInfoLength; j++) {
				if(chargingStationID == stationKWHInfoData[j].chargingStationID) {
					rowspanCount++;
				}
			}
			var rowSpan = true;
			for (var j=0; j<stationKWHInfoLength; j++) {
				if(chargingStationID == stationKWHInfoData[j].chargingStationID) {
					$tr = $('<tr>').appendTo(costBalanceBillTbody);
					if (rowSpan) {
						$('<td rowspan="'+ rowspanCount + '">').html(i+1).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(stationKWHInfoData[j].chargingStationName).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].checkMeterLastTime).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].checkMeterThisTime).appendTo($tr);
					}
					$('<td>').html(stationKWHInfoData[j].checkMeterCode).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].checkMeterRate).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].totalMeterLastNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].sharpMeterLastNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].peakMeterLastNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].flatMeterLastNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].valleyMeterLastNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].idleTotalLastNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].totalMeterThisNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].sharpMeterThisNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].peakMeterThisNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].flatMeterThisNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].valleyMeterThisNo).appendTo($tr);
					$('<td>').html(stationKWHInfoData[j].idleTotalThisNo).appendTo($tr);
					if (rowSpan) {
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].totalActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].sharpActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].peakActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].flatActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].valleyActiveKWH).appendTo($tr);
						
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].sharpProportion).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].peakProportion).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].flatProportion).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].valleyProportion).appendTo($tr);
						
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].payElectricCharge).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].totalChargingKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].stationRatio).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].costSubtotal).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].difference).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(meterInfoData[i].ratio).appendTo($tr);
						rowSpan = false;
					}
				}
			}
		}
		$("#save").attr('disabled', false);
	} else {
		$("#costBalanceBillThead").hide();
		$("#errorMessage").show();
		$("#errorMessage").html(messageContent["Balance.MonthMeterRecordNotFound"]);
		$("#save").attr('disabled', 'disabled');
	}
	setTimeout(hideLoading, 500);
}

function saveBillInfo(){
	if (!$("#BalanceBillName").val()) {
		$("#BalanceBillName").tips({
			side : 2,
			msg : "请输入电力结算单名称",
			bg : '#AE81FF',
			time : 1
		});
		$("#BalanceBillName").focus();
		return;
	}
	$ajax('saveBillInfo', {BalanceBillName: $("#BalanceBillName").val(),
							BalanceMonth: $("#balanceMonth").val()}, saveBillRecord);
}

function saveBillRecord(data){
	var result = data && data.result;
	if (result == "Success") {
		showMessge(messageContent["Balance.SaveBillSuccess"]);
		window.parent.skipSecondMenu('costManage/costBalanceBill', 'costBalanceBill');
	} else if (result == "BalanceBillNameExist"){
		showMessge(messageContent["Balance.BalanceBillNameExist"]);
	} else if (result == "DataError"){
		showMessge(messageContent["Balance.BalanceBillDataError"]);
	} else {
		showMessge(messageContent["Balance.SaveBillError"]);
	}
}
function goback() {
	window.parent.skipSecondMenu('costManage/costBalanceBill', 'costBalanceBill');
}